package com.train.chatspring.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MailService {

    @Autowired
    private JavaMailSender mailSender;

    // 新增: 从配置文件中读取发件人邮箱
    @Value("${spring.mail.username}")
    private String fromEmail;

    /**
     * 发送简单邮件
     *
     * @param to 收件人邮箱
     * @param subject 邮件主题
     * @param content 邮件正文
     */
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail); // 使用配置文件中的发件人邮箱
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    /**
     * 发送模板邮件
     *
     * @param to 收件人邮箱
     * @param subject 邮件主题
     * @param templateName 模板名称
     * @param model 模板参数
     */
    public void sendTemplateMail(String to, String subject, String templateName, Map<String, Object> model) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail); // 使用配置文件中的发件人邮箱
        message.setTo(to);
        message.setSubject(subject);
        message.setText("测试发送");
        mailSender.send(message);
    }
}
